import 'package:doctor_inquiry_lib/constant/constant.dart';

class WMessage {
  String customMsgID = '';
  String msgID;
  WMessageState state;
  bool isSend;

  //对方是否已读
  bool isPeerRead;
  WUserInfo from;
  WUserInfo target;

  // 发送消息时间
  int createTime;
  WMessageType msgType;

  WMessage(
      {this.msgID,
      this.state,
      this.isSend,
      this.from,
      this.target,
      this.isPeerRead,
      this.createTime,
      this.msgType});

  Map toJson() {
    return {
      'isPeerRead': isPeerRead,
      'uuid': msgID,
      'isSend': isSend,
      'from': from.toJson(),
      'createTime': createTime,
      'customMsgID': customMsgID,
      'target': target.toJson(),
      'type': msgType.toString()
    };
  }

  WMessage.fromJson(Map<dynamic, dynamic> json)
      : msgID = json['uuid'],
        createTime = json['createTime'],
        isSend = json['isSend'],
        isPeerRead = json['isPeerRead'],
        from = WUserInfo.fromJson(json['from']),
        target = WUserInfo.fromJson(json['target']),
        msgType = json['msgType'];
}

enum WMessageState {
  sending, // 正在发送中
  send_succeed, // 发送成功
  send_failed, // 发送失败
}

enum WMessageType {
  text,
  hint,
  sys,
  card,
  image,
  voice,
}

class WUserInfo {
  String id;
  String username;
  String nick;
  String headUrl;

  WUserInfo({this.id, this.username, this.nick, this.headUrl});

  Map toJson() {
    return {
      'id': id,
      'username': username,
      'nick': nick,
      'headUrl': headUrl,
    };
  }

  WUserInfo.fromJson(Map<dynamic, dynamic> json)
      : username = json['username'],
        id = json['id'],
        headUrl = json['headUrl'],
        nick = json['nick'];
}

class WTextMessage extends WMessage {
  String text;

  WTextMessage({this.text}) {
    msgType = WMessageType.text;
  }

  Map toJson() {
    var json = super.toJson();
    json['text'] = text;
    return json;
  }

  WTextMessage.fromJson(Map<dynamic, dynamic> json)
      : text = json['text'],
        super.fromJson(json);
}

///自定义卡片消息
class WCardMessage extends WMessage {
  //{"customType":"card","childType":"patientInfo",
  // "data":{"title":"您要为谁咨询","doctorID":"20031111131047917440531001","inquiryID":"20103017203114960552212211"}}
  String childType;
  String title;
  String doctorID;
  String inquiryID;

  WCardMessage({this.childType, this.title, this.doctorID, this.inquiryID}) {
    msgType = WMessageType.card;
  }

  Map toJson() {
    var json = super.toJson();
    json['childType'] = childType;
    json['title'] = title;
    json['doctorID'] = doctorID;
    json['inquiryID'] = inquiryID;
    return json;
  }

  WCardMessage.fromJson(Map<dynamic, dynamic> json)
      : childType = json['childType'],
        title = json['title'],
        doctorID = json['doctorID'],
        inquiryID = json['inquiryID'],
        super.fromJson(json);
}

///显示在中间的提示类消息
class WHintMessage extends WMessage {
  String hint;

  WHintMessage({this.hint}) {
    msgType = WMessageType.hint;
  }

  Map toJson() {
    var json = super.toJson();
    json['hint'] = hint;
    return json;
  }

  WHintMessage.fromJson(Map<dynamic, dynamic> json)
      : hint = json['hint'],
        super.fromJson(json);
}

class WSysMessage extends WMessage {
  //{"customType":"sys","data":{"startInquiry":true,"inquiryID":"20103017203114960552212211"}}
  //{"customType":"sys","childType":"end-inquiry","data":{}}
  String body;
  String data;
  String childType;

  WSysMessage({this.body, this.data, this.childType}) {
    msgType = WMessageType.sys;
  }

  Map toJson() {
    var json = super.toJson();
    json['body'] = body;
    json['data'] = data;
    json['childType'] = childType;
    return json;
  }

  WSysMessage.fromJson(Map<dynamic, dynamic> json)
      : body = json['body'] ?? '',
        childType = json['childType'] ?? '',
        data = json['data'] ?? '',
        super.fromJson(json);
}

class WImageMessage extends WMessage {
  String thumbPath = "";
  String path ="";
  String placeholder = "${Constant.ASSETS_IMG_CHAT}taozi-pic.png";
  // List<String> urls;

  WImageMessage() {
    msgType = WMessageType.image;
  }

  Map toJson() {
    var json = super.toJson();
    // json['urls'] = urls;
    json['thumbPath'] = thumbPath;
    json['path'] = path;
    return json;
  }

  WImageMessage.fromJson(Map<dynamic, dynamic> json)
      : thumbPath = json['thumbPath'] ?? '',
        // urls = json['urls'] ?? '',
        path = json['path'] ?? '',
        super.fromJson(json);
}

class WVoiceMessage extends WMessage {
  String path = ''; // 语音文件路径,如果为空需要调用相应下载方法，注意这是本地路径，不能是 url
  String remoteUrl;

  int duration; // 语音时长，单位秒

  WVoiceMessage({this.path, this.remoteUrl, this.duration}) {
    msgType = WMessageType.voice;
  }

  Map toJson() {
    var json = super.toJson();
    json['path'] = path;
    json['duration'] = duration;
    json['remoteUrl'] = remoteUrl;
    return json;
  }

  WVoiceMessage.fromJson(Map<dynamic, dynamic> json)
      : path = json['path'],
        duration = json['duration'],
        remoteUrl = json['remoteUrl'],
        super.fromJson(json);
}
